/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.locations.rooms;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ewerp.mud.characters.ICharacter;
import com.ewerp.mud.entities.IObjectId;
import com.ewerp.mud.entitycore.IEntityProperty;
import com.ewerp.mud.entitycore.descriptions.IDescription;
import com.ewerp.mud.entitycore.descriptions.meta.IDescriptionMeta;
import com.ewerp.mud.entitycore.gender.GenderFactory;
import com.ewerp.mud.entitycore.gender.IGender;
import com.ewerp.mud.entitycore.gender.Neutral;
import com.ewerp.mud.locations.exits.IExit;
import com.ewerp.mud.messages.IMessage;

public class BaseRoom implements IRoom {
    protected IObjectId id;
    
    protected List<IEntityProperty> properties = new ArrayList<IEntityProperty>();
    
    protected List<ICharacter> characters = new ArrayList<ICharacter>();
    protected Map<Class<? extends IDescriptionMeta>, IDescription> descriptions = new HashMap<Class<? extends IDescriptionMeta>, IDescription>();
    protected List<IExit> exits = new ArrayList<IExit>();

    public BaseRoom() {
    	id = new RoomId();
    }
    
    @Override
    public void addCharacter(ICharacter character) {
        characters.add(character);
    }

    @Override
    public void removeCharacter(ICharacter character) {
        characters.remove(character);
    }
    
    @Override
    public void dispatchMessage(IMessage message) {
        for(ICharacter character : characters) {
            character.handleMessage(message);
        }
    }

    @Override
    public IDescription getDescription(Class<? extends IDescriptionMeta> type) {
        return descriptions.get(type);
    }

    @Override
    public void setDescription(IDescription description) {
        descriptions.put(description.getMeta().getClass(), description);
    }

    @Override
    public List<IExit> getExits() {
        return exits;
    }

    @Override
    public IObjectId getId() {
        return id;
    }

    @Override
    public void setId(IObjectId roomId) {
        this.id = roomId;
    }
    
    @Override
	public IGender getGender() {
		return GenderFactory.getInstance(Neutral.class);
	}

	@Override
	public void setGender(IGender gender) {
	}
	
	@Override
	public List<IEntityProperty> getProperties() {
		return properties;
	}

	@Override
	public void setProperties(List<IEntityProperty> properties) {
		this.properties = properties;
	}
    
    public long getSerial() {
    	return id == null ? -1 : id.getSerial();
    }
    
    public void setSerial(long id) {
    	if(this.id == null) {
    		this.id = new RoomId();
    	}
    	
    	this.id.setSerial(id);
    }
    
    public String getRoomName() {
    	return id == null ? null : ((IRoomId)id).getName();
    }
    
    public void setRoomName(String name) {
    	if(id == null) {
    		id = new RoomId();
    	}
    	((IRoomId)id).setName(name);
    }

    public Map<Class<? extends IDescriptionMeta>, IDescription> getDescriptions() {
    	return descriptions;
    }
    
    public void setDescriptions(Map<Class<? extends IDescriptionMeta>, IDescription> descriptions) {
    	this.descriptions = descriptions;
    }
	
}
